<template>
  <div>
    <el-card class="box-1">
      <div slot="header"><span>{{ title }}</span></div>
      <el-form ref="form" :model="form" :rules="formRules" label-width="160px">
        <el-form-item label="售后人员">
          <el-select :disabled="pageType==0" v-model="form.after_sale_id" placeholder="请选择售后人员" filterable>
            <el-option v-for="item in usersList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="客户名称">
          <el-input v-model="form.member_name" :readonly="title!=='售后订单-创建'"/>
        </el-form-item>
        <el-form-item label="客户手机" prop="member_phone">
          <el-input v-model="form.member_phone" :readonly="title!=='售后订单-创建'"/>
        </el-form-item>
        <el-form-item label="商品名称" prop="goods_name">
          <el-select :disabled="pageType==0" v-model="form.goods_idx" placeholder="请选择商品" filterable @change="change">
            <el-option v-for="(item, index) in goodsList" :key="index" :label="item.goods_name" :value="index" />
          </el-select>
          <el-select v-if="form.goods_idx!==''&&goodsList[form.goods_idx].goods_version.length>0" :disabled="pageType==0" v-model="form.goods_version_idx" placeholder="请选择版本" filterable>
            <el-option v-for="(item, index) in goodsList[form.goods_idx].goods_version" :key="index" :label="item.goods_version" :value="index" />
          </el-select>
          <el-select v-if="form.goods_version_idx!==''&&goodsList[form.goods_idx].goods_version[form.goods_version_idx].subitem.length>0" :disabled="pageType==0" v-model="form.goods_subitem_idx" placeholder="请选择规格" filterable @change="selectGoods">
            <el-option v-for="(item, index) in goodsList[form.goods_idx].goods_version[form.goods_version_idx].subitem" :key="index" :label="item.goods_version" :value="index" />
          </el-select>
        </el-form-item>
        <el-form-item label="商品原价">
          <el-input :readonly="pageType==0" v-model="form.price" />
        </el-form-item>
        <el-form-item label="实际销售价" prop="goods_money">
          <el-input :readonly="pageType==0" v-model="form.goods_money" type="number" />
        </el-form-item>
        <el-form-item label="订单号" prop="order_num">
          <el-input :readonly="pageType==0" v-model="form.order_number" placeholder="请填写正确订单号，无则填写合同号"/>
        </el-form-item>
        <el-form-item label="购买时间">
          <el-date-picker :readonly="pageType==0" v-model="form.buy_time" value-format="yyyy-MM-dd" type="date" placeholder="选择日期" />
        </el-form-item>
        <el-form-item label="购买时长" prop="term">
          <el-input :readonly="pageType==0" v-model="form.buy_length" />
          <div class="remark">*注:时长是按月计算</div>
        </el-form-item>
        <el-form-item label="是否有奖金">
          <el-radio-group :disabled="pageType==0" v-model="form.after_type">
            <el-radio :label="0">有奖金</el-radio>
            <el-radio :label="2">固定奖金</el-radio>
            <el-radio :label="1">无奖金</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form.after_type === 0" label="奖金规则">
          <el-select :disabled="pageType==0" v-model="form.sbr_id" placeholder="请选择奖金规则">
            <el-option v-for="item in ruleList" :key="item.id" :label="item.rule_name" :value="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.after_type === 2" label="固定奖金金额" prop="after_money">
          <el-input :readonly="pageType==0" v-model="form.after_money" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input :readonly="pageType==0" v-model="form.remarks" type="textarea" rows="4"/>
        </el-form-item>
        <div v-if="pageType!=0" style="text-align:center">
          <el-button :disabled="loading" :loading="loading" type="primary" @click="submitForm('form')">保存</el-button>
        </div>
        <!--<div v-if="pageType==0&&queryMatch(96)" style="text-align:center">
          <el-button type="primary" @click="pageType=1">编辑</el-button>
        </div>-->
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { validatOnlyNumberOrLetter } from '@/utils/validate';
export default {
  data() {
    const checkCommission = (rule, value, callback) => {
      if (value === '' && this.form.isCommission === 1) {
        callback(new Error('请输入售前提成金额'))
      } else {
        callback()
      }
    }
    return {
      loading: false,
      title: '',
      pageType: 0,

      usersList: [], // 系统用户列表
      goodsList: [], // 商品列表
      ruleList: [], // 规则列表

      form: {
        price: '',
        goods_idx: '',
        goods_version_idx: '',
        goods_subitem_idx: ''
      },
      formRules: {
        commission_before: [
          { validator: checkCommission, trigger: 'blur' }
        ]
      }
    }
  },
  watch: {},
  created() {
    this.title = this.$route.query.title
    this.$route.query.id && (this.id = this.$route.query.id)
    this.$route.query.id && this.getData()
    this.getUsersList()
    this.getGoodsList()
    this.getRuleList()
    if (this.title === '售后订单-详情') {
      this.pageType = 0
    } else {
      this.pageType = 1
    }
  },
  methods: {
    change(){
      this.form.goods_version_idx = '';
      this.form.goods_subitem_idx = '';
    },
    selectGoods() {
      this.form.price = this.goodsList[this.form.goods_idx].goods_version[this.form.goods_version_idx].subitem[this.form.goods_subitem_idx].originalPrice;
      this.form.goods_name = this.goodsList[this.form.goods_idx].goods_name +
        '--' + this.goodsList[this.form.goods_idx].goods_version[this.form.goods_version_idx].goods_version +
        '--' + this.goodsList[this.form.goods_idx].goods_version[this.form.goods_version_idx].subitem[this.form.goods_subitem_idx].goods_version
    },
    getData() {
      const url = 'aftersale/detail/' + this.id
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.form = res.data
        this.form.goods_idx = '';
        this.form.goods_version_idx = '';
        this.form.goods_subitem_idx = '';
        const temp = this.form.goods_name.split('--');
        for (let i = 0; i < this.goodsList.length; i++) {
          if (temp[0] && this.goodsList[i].goods_name === temp[0]) {
            this.form.goods_idx = i;
            for (let j = 0; j < this.goodsList[i].goods_version.length; j++) {
              if (temp[1] && this.goodsList[i].goods_version[j].goods_version === temp[1]) {
                this.form.goods_version_idx = j;
                for (let k = 0; k < this.goodsList[i].goods_version[j].subitem.length; k++) {
                  if (temp[2] && this.goodsList[i].goods_version[j].subitem[k].goods_version === temp[2]) {
                    this.form.goods_subitem_idx = k;
                  }
                }
              }
            }
          }
        }
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    /**
     *  获取系统用户列表
     *  接口:'user/all-list'
     */
    getUsersList() {
      const url = 'user/all-list'
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.usersList = res.data
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    /**
     *  获取商品列表
     *  接口:'goods/all-list'
     */
    getGoodsList() {
      const url = 'goods/all-list'
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.goodsList = res.data
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    /**
     *  获取规则列表
     *  接口:'salerule/all-list'
     */
    getRuleList() {
      const url = 'salerule/all-list'
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.ruleList = res.data
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = this.form
          if (!validatOnlyNumberOrLetter(data['order_number'])) {
            this.$message.error('订单号只能为字母及数字');
            return;
          }
          this.loading = true;
          const url = this.id ? 'aftersale/edit/' + this.id : 'aftersale/create'
          this.$store.dispatch('GetConnect', { url, data }).then(res => {
            this.$message.success(res.msg)
            this.$router.back(-1)
          }).catch((e) => {
            this.loading = false;
            this.$message.error(e.msg)
          })
        } else {
          this.$message.error('提交失败,请检查必填项')
        }
      })
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../styles/index.scss';
.box-1 {
  max-width: 1000px;
  margin: 0 auto;
  > div span {
    font-size: 18px;
    font-weight: 600;
    padding: 0 10px;
  }
  .el-form {
    max-width: 900px;
    .title {
      font-weight: 600;
    }
    .remark {
      font-size: 13px;
      color: $textGray;
    }
    .el-button {
      @extend .bg-green;
      width: 20%;
      min-width: 160px;
    }
  }
}
.el-checkbox {
  margin-right: 30px;
}
.el-checkbox + .el-checkbox {
  margin-left: 0;
  margin-right: 30px;
}
</style>
